package java44th.group.service.admin.user;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java44th.group.bean.common.BasePageInfo;
import java44th.group.bean.common.CommonData;
import java44th.group.bean.po.MarketUser;
import java44th.group.bean.po.MarketUserExample;
import java44th.group.mapper.MarketUserMapper;
import java44th.group.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ProjectName project-second
 * @Descriptiom TODO
 * @Aurhor: 蔡文强
 * @Since: 2022/10/12 13:49
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    MarketUserMapper userMapper;


    /**
     * @param info     分页page\limit   排序sort\WxOrderController
     * @param username
     * @param mobile
     * @return CommonData<MarketUser>
     * @description: TODO
     * @author caicaijiang
     * @date 2022/10/12 14:05
     */
    @Override
    public CommonData<MarketUser> list(BasePageInfo info, String username, String mobile) {

        // 开启分页
        PageHelper.startPage(info.getPage(), info.getLimit());


        //只根据username和mobile做条件查询
        // where username like ? and mobile = ?
        //先不考虑username和mobile是否为空 → 接着考虑username是否为空，mobile是否为空
        // Criterion的list
        MarketUserExample example = new MarketUserExample();
        // 排序
        // getsort根据添加的时间进行排序
        example.setOrderByClause(info.getSort() + " " + info.getOrder());


        MarketUserExample.Criteria criteria = example.createCriteria();
        // 增加if之后变为条件查询
        if (!StringUtil.isEmpty(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (!StringUtil.isEmpty(mobile)) {
            criteria.andMobileEqualTo(mobile);
        }


        // 在查询之前需要开启分页!!!!
        List<MarketUser> marketUsers = userMapper.selectByExample(example);

        // 开启分页之后获取分页信息
        PageInfo<MarketUser> marketUserPageInfo = new PageInfo<>(marketUsers);


        /*int pageNum = marketUserPageInfo.getPageNum();// page
        int pageSize = marketUserPageInfo.getPageSize(); // limit
        long total = marketUserPageInfo.getTotal();// total
        int pages = marketUserPageInfo.getPages();//pages
        List<MarketUser> list = marketUserPageInfo.getList();// 相当于获得marketUsers
        CommonData<MarketUser> data = new CommonData<>((int) total, pages, pageSize, pageNum, list);*/
        return CommonData.data(marketUserPageInfo);
    }

    @Override
    public MarketUser queryUserById(Integer id) {

        MarketUser marketUser = userMapper.selectByPrimaryKey(id);

        return marketUser;
    }

    @Override
    public void modifyUserUserObject(MarketUser marketUser) {

        userMapper.updateByPrimaryKeySelective(marketUser);

    }
}
